package com.slibra.quartz.task;

import cn.hutool.core.date.DateUtil;
import cn.hutool.http.HttpRequest;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONArray;
import com.slibra.business.domain.TXinyiIndustry;
import com.slibra.business.domain.TXinyiRobot;
import com.slibra.business.mapper.TXinyiIndustryMapper;
import com.slibra.business.mapper.TXinyiRobotMapper;
import com.slibra.common.enums.DataSourceType;
import com.slibra.common.utils.DateUtils;
import com.slibra.framework.datasource.DynamicDataSourceContextHolder;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import com.slibra.common.utils.StringUtils;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StopWatch;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;
import java.util.concurrent.TimeUnit;

/**
 * 定时任务调度测试
 * 
 * 
 */
@Component("ryTask")
@Slf4j
public class RyTask
{
    public void ryMultipleParams(String s, Boolean b, Long l, Double d, Integer i)
    {
        System.out.println(StringUtils.format("执行多参方法： 字符串类型{}，布尔类型{}，长整型{}，浮点型{}，整形{}", s, b, l, d, i));
    }

    public void ryParams(String params)
    {
        System.out.println("执行有参方法：" + params);
    }

    public void ryNoParams()
    {
        System.out.println("执行无参方法");
    }

    //----------------------------------------------下面是新增的方法----------------------------------------------

    @Autowired
    private TXinyiIndustryMapper xinyiIndustryMapper;

    @Autowired
    private TXinyiRobotMapper xinyiRobotMapper;
//    public final static StopWatch watch = new StopWatch("task");
    public static final String[] queryTags = {"信义污水厂JS_COD_Value","信义污水厂JS_PH_Value","信义污水厂JS_SS_Value","信义污水厂JS_ZL_Value","信义污水厂JS_ZA_Value","信义污水厂JS_AD_Value","信义污水厂JS_T_Value","信义污水厂进水泵房液位","信义污水厂出水瞬时流量","信义污水厂升级出水COD","信义污水厂升级出水PH","信义污水厂升级出水SS","信义污水厂升级出水TN","信义污水厂升级出水TP","信义污水厂升级出水氨氮","信义污水厂AIT202_Value","信义污水厂AIT203_Value","信义污水厂AIT207_Value","信义污水厂AIT206_Value","信义污水厂AIT209_Value","信义污水厂AIT210_Value","信义污水厂进水TDS","信义污水厂FT101_Value","信义污水厂SWCHHYHLB1_R_Value","信义污水厂SWCHHYHLB2_R_Value","信义污水厂SWCHHYHLB3_R_Value","信义污水厂SWCHHYHLB4_R_Value","信义污水厂SWCHHYHLB5_R_Value","信义污水厂SWCHHYHLB6_R_Value","信义污水厂SWCWNHLB1_R_Value","信义污水厂SWCWNHLB2_R_Value","信义污水厂SWCWNHLB3_R_Value","信义污水厂SWCWNHLB4_R_Value","信义污水厂SWCWNHLB5_R_Value","信义污水厂GFJ1_R_Value","信义污水厂GFJ2_R_Value","信义污水厂GFJ3_R_Value","信义污水厂GFJ4_R_Value","信义污水厂GFJ5_R_Value","信义污水厂GFJ6_R_Value","信义污水厂GFJ1_KQLL_Value","信义污水厂GFJ2_KQLL_Value","信义污水厂GFJ3_KQLL_Value","信义污水厂GFJ4_KQLL_Value","信义污水厂GFJ5_KQLL_Value","信义污水厂GFJ6_KQLL_Value","信义污水厂实际碳源加药量","信义污水厂除磷加药瞬时流量", "信义污水厂_除磷P04预测值_"};

    /**
     * 定时从工业库获取数据
     *
     * 2024年4月17日17:44:15 调整逻辑：考虑到因断电等情况导致服务断电，所以不再同步最近一小时，而是同步从上次成功的最后一条数据开始。
     */
    public void getIndustryData(){
        log.info("进入了定时同步工业库数据的任务");

        //耗时工具
//        watch.start("parseJob");


        // 给定时间段的起始时间和结束时间
        LocalDateTime endTime =  LocalDateTime.now();
        //todo
//        LocalDateTime startTime = endTime.plusMinutes(-60);
        //获取上次最后一条同步的数据的日期到 分钟维度
        String lastDateHour = this.xinyiIndustryMapper.getLastMinute();
        log.info("获取上次同步工业库的最后一条记录的时间是{}", lastDateHour);
        lastDateHour = lastDateHour + ":00";
        //开始时间
        LocalDateTime startTime = LocalDateTime.parse(lastDateHour.replaceAll("/", "-").replace(" ", "T"));
        startTime = startTime.plusMinutes(1L);//加一分钟  从上次最后一条记录的下一分钟开始
        /*LocalDateTime startTime = LocalDateTime.parse("2024-02-26T00:00:00");
        LocalDateTime endTime = LocalDateTime.parse("2024-02-27T00:00:00");*/

        // 每个小时的时间格式
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

        // 循环按小时分割
        LocalDateTime currentHour = startTime;
        while (currentHour.isBefore(endTime)) {
            String begin = currentHour.format(formatter);
            String end = currentHour.plusMinutes(5).format(formatter);
            // 输出当前小时的起始时间和结束时间
            System.out.println("起始时间：" + begin);
            System.out.println("结束时间：" + end);

            // 当前小时加一小时，作为下一个小时的起始时间
            currentHour = currentHour.plusMinutes(5);

            //每个小时查询一次数据
            String url = "http://10.0.0.27:4568/api/v1/khquerydata";
            HashMap<String, Object> req = new HashMap<>();
            req.put("tagNames", queryTags);
            req.put("startTime", begin);
            req.put("endTime", end);
            req.put("recordNumbers", 100000);
            String body = HttpRequest.post(url).header("Authorization", "c2E6c2E=").header("clientName", "hongshan").body(JSON.toJSONString(req)).execute().body();
//            System.out.println("body = " + body);
            List<HashMap<String, String>> list = new ArrayList<>();

            //行转列数据处理
            for (String queryTag : queryTags) {
                JSONArray array = JSON.parseObject(body).getJSONArray(queryTag);
                //特殊数据处理一
                if(Objects.isNull(array) || array.isEmpty()){
                    System.out.println(queryTag + "查询到了空的数据，跳过本次循环");
                    continue;
                }
                int size = array.size();
                //特殊数据处理二
                if("0".equals(array.get(1) + "")){
                    System.out.println(queryTag + "查询到了数据，但是数据集合只有一条，且都是0");
                    continue;
                }
                //结合至少62个数据才满足条件(有可能获取不到)
                /*if(size < 62){
                    System.out.println(queryTag + "查询到了不符合条件的数据，跳过本次循环");
                    continue;
                }*/

                //存放的数据集
                //利用map去重
                HashMap<String, String> map = new LinkedHashMap<>();
                for (int i = 2; i < size; i++) {
//            System.out.println(i + "" + array.get(i));
                    JSONArray oneRecord = JSON.parseArray(JSON.toJSONString(array.get(i)));
                    //处理为空或者为0的数据
                    Object timeStampValue = oneRecord.get(2);
                    if(Objects.isNull(timeStampValue) || "0".equals(timeStampValue + ""))
                        continue;
                    BigDecimal value = Objects.isNull(oneRecord.get(0)) ? null : new BigDecimal(oneRecord.get(0) + "");
                    long timestamp = (long) timeStampValue;
                    String format = DateUtil.format(new Date(timestamp), DateUtils.YYYYMMDDHHMM_TS);
                    map.put(format, queryTag + "-" + value);
                }
                list.add(map);
            }

            Set<String> recordTimeSet = new HashSet<>();
            Map<String, TXinyiIndustry> recordMap = new HashMap<>();
            for (int i = 0; i < list.size(); i++) {
                HashMap<String, String> map = list.get(i);
                int finalJ = i;
                map.forEach((k, v) ->{
                    TXinyiIndustry industry = null;
                    if(!recordTimeSet.contains(k)){//第一次
                        industry = new TXinyiIndustry();
                        recordTimeSet.add(k);
                        recordMap.put(k, industry);
                    }else{
                        industry = recordMap.get(k);
                    }
                    industry.setTestTime(k);
                    //2024年4月15日11:19:52  额外增加2个字段
                    industry.setTestDate(k.substring(0,10));
                    industry.setTestHour(k.substring(0,13));
                    //解析值
                    String[] split = v.split("-");
                    String type = split[0];
                    BigDecimal value = new BigDecimal(split[1]);
                    if ("信义污水厂JS_COD_Value".equals(type)) {
                        industry.setJsCod(value);
                    } else if ("信义污水厂JS_PH_Value".equals(type)) {
                        industry.setJsPh(value);
                    } else if ("信义污水厂JS_SS_Value".equals(type)) {
                        industry.setJsSs(value);
                    } else if ("信义污水厂JS_ZL_Value".equals(type)) {
                        industry.setJsTp(value);
                    } else if ("信义污水厂JS_ZA_Value".equals(type)) {
                        industry.setJsTn(value);
                    } else if ("信义污水厂JS_AD_Value".equals(type)) {
                        industry.setJsNh3(value);
                    } else if ("信义污水厂JS_T_Value".equals(type)) {
                        industry.setJsSwPh(value);
                    } else if ("信义污水厂进水泵房液位".equals(type)) {
                        industry.setJsBfyw(value);
                    } else if ("信义污水厂出水瞬时流量".equals(type)) {
                        industry.setCsSlqc(value);
                    } else if ("信义污水厂升级出水COD".equals(type)) {
                        industry.setCsCod(value);
                    } else if ("信义污水厂升级出水PH".equals(type)) {
                        industry.setCsPh(value);
                    } else if ("信义污水厂升级出水SS".equals(type)) {
                        industry.setCsSs(value);
                    } else if ("信义污水厂升级出水TN".equals(type)) {
                        industry.setCsTn(value);
                    } else if ("信义污水厂升级出水TP".equals(type)) {
                        industry.setCsTp(value);
                    } else if ("信义污水厂升级出水氨氮".equals(type)) {
                        industry.setCsNh3(value);
                    } else if ("信义污水厂AIT202_Value".equals(type)) {
                        industry.setOneHyzdDo(value);
                    } else if ("信义污水厂AIT203_Value".equals(type)) {
                        industry.setOneHymdDo(value);
                    } else if ("信义污水厂AIT207_Value".equals(type)) {
                        industry.setTwoHyzdDo(value);
                    } else if ("信义污水厂AIT206_Value".equals(type)) {
                        industry.setTwoHymdDo(value);
                    } else if ("信义污水厂AIT209_Value".equals(type)) {
                        industry.setOneMlss(value);
                    } else if ("信义污水厂AIT210_Value".equals(type)) {
                        industry.setTwoMlss(value);
                    } else if ("信义污水厂进水TDS".equals(type)) {
                        industry.setJsTds(value);
                    } else if ("信义污水厂FT101_Value".equals(type)) {
                        industry.setJsSlq(value);
                    } else if ("信义污水厂SWCHHYHLB1_R_Value".equals(type)) {
                        industry.setNHlbOneGp(value);
                    } else if ("信义污水厂SWCHHYHLB2_R_Value".equals(type)) {
                        industry.setNHlbTwoGp(value);
                    } else if ("信义污水厂SWCHHYHLB3_R_Value".equals(type)) {
                        industry.setNHlbThreeGp(value);
                    } else if ("信义污水厂SWCHHYHLB4_R_Value".equals(type)) {
                        industry.setNHlbFourGp(value);
                    } else if ("信义污水厂SWCHHYHLB5_R_Value".equals(type)) {
                        industry.setNhlBFiveGp(value);
                    } else if ("信义污水厂SWCHHYHLB6_R_Value".equals(type)) {
                        industry.setNHlbSixGp(value);
                    } else if ("信义污水厂SWCWNHLB1_R_Value".equals(type)) {
                        industry.setWHlbOneGp(value);
                    } else if ("信义污水厂SWCWNHLB2_R_Value".equals(type)) {
                        industry.setWHlbTwoGp(value);
                    } else if ("信义污水厂SWCWNHLB3_R_Value".equals(type)) {
                        industry.setWHlbThreeGp(value);
                    } else if ("信义污水厂SWCWNHLB4_R_Value".equals(type)) {
                        industry.setWHlbFourGp(value);
                    } else if ("信义污水厂SWCWNHLB5_R_Value".equals(type)) {
                        industry.setWHlbFiveGp(value);
                    } else if ("信义污水厂GFJ1_R_Value".equals(type)) {
                        industry.setFjOne(value);
                    } else if ("信义污水厂GFJ2_R_Value".equals(type)) {
                        industry.setFjTwo(value);
                    } else if ("信义污水厂GFJ3_R_Value".equals(type)) {
                        industry.setFjThree(value);
                    } else if ("信义污水厂GFJ4_R_Value".equals(type)) {
                        industry.setFjFour(value);
                    } else if ("信义污水厂GFJ5_R_Value".equals(type)) {
                        industry.setFjFive(value);
                    } else if ("信义污水厂GFJ6_R_Value".equals(type)) {
                        industry.setFjSix(value);
                    } else if ("信义污水厂GFJ1_KQLL_Value".equals(type)) {
                        industry.setKqllOne(value);
                    } else if ("信义污水厂GFJ2_KQLL_Value".equals(type)) {
                        industry.setKqllTwo(value);
                    } else if ("信义污水厂GFJ3_KQLL_Value".equals(type)) {
                        industry.setKqllThree(value);
                    } else if ("信义污水厂GFJ4_KQLL_Value".equals(type)) {
                        industry.setKqllFour(value);
                    } else if ("信义污水厂GFJ5_KQLL_Value".equals(type)) {
                        industry.setKqllFive(value);
                    } else if ("信义污水厂GFJ6_KQLL_Value".equals(type)) {
                        industry.setKqllSix(value);
                    }else if ("信义污水厂实际碳源加药量".equals(type)) {
                        industry.setSJTYJLY(value);
                    }else if ("信义污水厂除磷加药瞬时流量".equals(type)) {
                        industry.setCLJYSSLL(value);
                    } else if ("信义污水厂_除磷P04预测值_".equals(type)) {
                        industry.setCLP04YCZ(value);
                    }
                    //只有最后一次才执行数据库添加
                    if(finalJ == list.size()-1){
                        //2024年4月22日15:45:24  额外保存两个字段 数组
                        List<BigDecimal> extraList = new ArrayList<>();
                        extraList.add(industry.getOneHymdDo());
                        extraList.add(industry.getTwoHymdDo());
                        industry.setHycRjyAll(JSON.toJSONString(extraList));
                        extraList.clear();
                        extraList.add(industry.getOneHyzdDo());
                        extraList.add(industry.getTwoHyzdDo());
                        industry.setHycRjyZdAll(JSON.toJSONString(extraList));
                        extraList.clear();
                        extraList.add(industry.getOneMlss());
                        extraList.add(industry.getTwoMlss());
                        industry.setHycWnndAll(JSON.toJSONString(extraList));
                        //插入数据库
                        xinyiIndustryMapper.insertTXinyiIndustry(industry);
                    }
                });

            }

        }

        //执行完成 测试执行时间
        //计时结束
//        watch.stop();
//        System.out.println(watch.getLastTaskName() + " 执行耗时：" + watch.getLastTaskTimeMillis() + " ms");
    }


    /**
     * 定时从sqlserver获取数据
     */
    public void sqlserverData(){
        log.info("进入了定时同步SqlServer的任务");
        //主库获取上次最新的同步日期
        String lastTime = this.xinyiRobotMapper.selectLastTime();
        log.info("上次同步的日期是{}", lastTime);
        //从
        DynamicDataSourceContextHolder.setDataSourceType(DataSourceType.SLAVE.name());
        List<TXinyiRobot> tXinyiRobots = xinyiRobotMapper.selectTXinyiRobotListByTime(lastTime);
        DynamicDataSourceContextHolder.clearDataSourceType();
//        System.out.println(JSON.toJSONString(tXinyiRobots));
//        System.out.println("-------------");
        //主
        if(!CollectionUtils.isEmpty(tXinyiRobots)){
            for (TXinyiRobot tXinyiRobot : tXinyiRobots) {
                String date = handleDate(tXinyiRobot.getVDate().replaceAll(" ", ""));//有空格
                String time = handleDate(tXinyiRobot.getVTime().replaceAll(" ", ""));//有空格
                tXinyiRobot.setVDate(date);
                tXinyiRobot.setVTime(time);
                tXinyiRobot.setVDateTime(date + " " + time);
                //处理给前端展示的字段
                tXinyiRobot.setTestDate(date);//日期
                tXinyiRobot.setTestHour(date + " " + time.substring(0, 2));//小时
                tXinyiRobot.setTestTime(date + " " + time.substring(0, 5));//分钟
                tXinyiRobot.setCreatedTime(new Date());
                this.xinyiRobotMapper.insertTXinyiRobot(tXinyiRobot);
            }
        }

    }



    public static String handleDate(String str){
        StringBuilder sb = new StringBuilder();
        if(str.contains(" ")){//包含空格 就是年月日时分秒了
            String[] split = str.split(" ");
            addBeforeZero(sb, split[0], "/");
            sb.append(" ");
            addBeforeZero(sb, split[1], ":");
        }else{
            if(str.contains("/")){//年月日
                addBeforeZero(sb, str, "/");
            }else if(str.contains(":")){//时分秒
                addBeforeZero(sb, str, ":");
            }else {
                sb.append(str);
            }
        }
        return sb.toString();
    }

    public static StringBuilder addBeforeZero(StringBuilder sb, String str, String tag){
        String[] split = str.split(tag);
        int length = split.length;
        for (int i = 0; i < length; i++) {
            String value = split[i];
            Integer intValue = Integer.parseInt(value);
            if(intValue < 10 && value.length() == 1){////防止有正确的情况 额外再补充字符串
                sb.append(0).append(value);
            }else{
                sb.append(value);
            }
            if(i < length-1){
                sb.append(tag);
            }
        }
        return sb;
    }

    public static void main(String[] args) {
        /*LocalDateTime endTime = LocalDateTime.now();
        System.out.println("endTime = " + endTime);
        endTime = endTime.plusMinutes(60);
        System.out.println("endTime = " + endTime);*/
        /*String str = "2024/04/15 09:55";
        System.out.println(str);
        System.out.println(str.substring(0,10));
        System.out.println(str.substring(0,13));*/

        /*String str = "2024/04/18 08:00";
        str = str + ":00";
        System.out.println(str);
        LocalDateTime startTime = LocalDateTime.parse(str.replaceAll("/", "-").replace(" ", "T"));
        System.out.println(startTime.plusMinutes(1L).format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));*/

        ArrayList<Integer> objects = new ArrayList<>();
        objects.add(1);
        objects.add(2);
        objects.add(3);
        System.out.println(objects);
        objects.clear();
        System.out.println(objects);

    }
}
